//
//  Date+Extension.swift
//  SK_BasePrivateProject
//
//  Created by Regina on 2018/10/08.
//  Copyright © 2018年 Regina. All rights reserved.
//

import UIKit

extension Date {
    
    ///获取日期具体组成（年、月、日、时、分、秒、 星期）,默认为当天
    public func components() -> (year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int, weekday: Int) {
        let calendar = NSCalendar.current
        let com = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second, .weekday], from: self)
        return (com.year!, com.month!, com.day!, com.hour!, com.minute!, com.second!, com.weekday!)
    }
    
    ///当月天数
    public func countOfDaysInMonth() -> Int {
        let calendar = Calendar(identifier:Calendar.Identifier.gregorian)
        let range = (calendar as NSCalendar?)?.range(of: NSCalendar.Unit.day, in: NSCalendar.Unit.month, for: self)
        return (range?.length)!
    }
    
    /// 当月第一天是星期几
    public func firstWeekDay() -> Int {
        //1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
        let calendar = Calendar(identifier:Calendar.Identifier.gregorian)
        let firstWeekDay = (calendar as NSCalendar?)?.ordinality(of: NSCalendar.Unit.weekday, in: NSCalendar.Unit.weekOfMonth, for: self)
        return firstWeekDay! - 1
    }
    
    ///相差天数,默认与当天比较
    public func dayDifference(fromDate: Date = Date()) -> Int {
        
        let timeInterval = self.timeIntervalSince(fromDate)
        let days = (Int(timeInterval)/(60*60*24))
        return days
    }
    
    ///相差月数,默认与当月比较
    public func monthDifference(fromDate: Date = Date()) -> Int {
        
        let calendar = NSCalendar.current
        let comFrom = calendar.dateComponents([.year, .month], from: fromDate)
        let com = calendar.dateComponents([.year, .month,], from:self)
        return (com.year! - comFrom.year!)*12 + (com.month! -  comFrom.month!)
    }
    
    ///相差年数,默认与当年比较
    public func yearDifference(fromDate: Date = Date()) -> Int {
        
        let calendar = NSCalendar.current
        let comFrom = calendar.dateComponents([.year, .month], from: fromDate)
        let com = calendar.dateComponents([.year, .month,], from:self)
        return com.year! - comFrom.year!
    }
    
    /**
     *  获取日期
     *
     *  @param  difference 相差天数
     *  @param  formatter  返回日期格式
     *
     *  @return 日期及日期字符串
     */
    public func day(difference: Int, formatter: String = "yyyy-MM-dd") -> (date: Date, dateString: String) {
        
        let calendar = Calendar.init(identifier: Calendar.Identifier.gregorian)
        var dateComponents = DateComponents()
        dateComponents.day = difference;
        var date = calendar.date(byAdding: dateComponents, to: self)
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = formatter
        
        let dateString = dateFormatter.string(from: date!)
        date = dateFormatter.date(from: dateString)
        
        return (date!, dateString)
    }
    /**
     *  获取日期
     *
     *  @param  difference 相差月数
     *  @param  formatter  返回日期格式
     *
     *  @return 日期及日期字符串
     */
    public func month(difference: Int, formatter: String = "yyyy-MM") -> (date: Date, dateString: String) {
        
        let calendar = Calendar.init(identifier: Calendar.Identifier.gregorian)
        var dateComponents = DateComponents()
        dateComponents.month = difference;
        var date = calendar.date(byAdding: dateComponents, to: self)
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = formatter
        let dateString = dateFormatter.string(from: date!)
        date = dateFormatter.date(from: dateString)

        return (date!, dateString)
    }
    /**
     *  获取日期
     *
     *  @param  difference 相差年数
     *  @param  formatter  返回日期格式
     *
     *  @return 日期及日期字符串
     */
    public func year(difference: Int, formatter: String = "yyyy-MM") -> (date: Date, dateString: String) {
        
        let calendar = Calendar.init(identifier: Calendar.Identifier.gregorian)
        var dateComponents = DateComponents()
        dateComponents.year = difference;
        var date = calendar.date(byAdding: dateComponents, to: self)
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = formatter
        let dateString = dateFormatter.string(from: date!)
        date = dateFormatter.date(from: dateString)

        return (date!, dateString)
    }
    
    ///将日期时间转为格式字符串
    public func toDateString(formatter: String) -> String {
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = formatter
        dateFormatter.timeZone = NSTimeZone.local
        let dateString = dateFormatter.string(from: self)
        return dateString
    }
    
    ///将时间字符串转为日期时间
    static public func dateStringToDate(dateString: String, formatter: String) -> Date {
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = formatter
        dateFormatter.timeZone = NSTimeZone.local
        let date = dateFormatter.date(from: dateString)
        return date!
    }
    
    ///时间转为时间戳
    public func toTimeStamp() -> Int {
        return Int(self.timeIntervalSince1970);
    }
    
    ///时间戳转为日期时间
    static public func timeStampToDate(timeStamp: String, formatter: String) -> (date: Date, dateString: String) {
        
        let timeSta:TimeInterval = NSString(string: timeStamp).doubleValue
        let date = Date(timeIntervalSince1970: timeSta)
        let dateFormatter = DateFormatter()
        
        dateFormatter.dateFormat = formatter
        
        let dateString = dateFormatter.string(from: date)
        return (date, dateString)
    }
    
    //月份数字转汉字
    static public func monthToHanzi(monthNum: Int) -> String {
        let chinaArray = ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
        let chinaStr:String = chinaArray[monthNum - 1]
        return chinaStr
    }
}
